﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BahdlClassLib;
using BusinessLayer;
using BusinessLayer.DTO;
using CrystalDecisions.CrystalReports.Engine;

namespace PayRoll
{
    public partial class SalaryGeneration : TransactionMasterForm
    {
        public SalaryGeneration()
        {
            InitializeComponent();
        }
        ClsSalaryGeneration ObjClsSalaryGeneration = null;
        private void SalaryGeneration_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            txtEmployee.Tag = 0;
            txtSalaryProcess.Tag = 0;
            ResetForm();
        }
        private void ResetForm()
        {
            lblbalanceLeave.Text = "";
            lblDepartment.Text = "";
            lblDesignation.Text = "";
            lblEmploymentName.Text = "";
            lblEmploymentStatus.Text = "";
           // lblGender.Text = "";
            lblHourlyRate.Text = "";
            lblJoinigDate.Text = "";
           // lblMaritalStatus.Text = "";
            lblName.Text = "";
            lblOverTimeRate.Text = "";
            lblSalaried.Text = "";
            lblTotalLeave.Text = "";
            chkDeductionApplied.Enabled = false;
            chkDeductionApplied.Checked = false;
            

            lblTotalNoOfWorkingdays.Text = "";
            lblTotalDaysWorked.Text = "";
            lblTotalDaysOff.Text = "";
            lblDaysWithOverTime.Text = "";
            lblOverTimeWorked.Text = "";
            lblTotalDays.Text = "";
            lblTotalDaysInThisMonth.Text = "";
            ObjClsSalaryGeneration = new ClsSalaryGeneration();
            grdSalaryComponent.DataSource = ObjClsSalaryGeneration.SalaryGenarationInfo;
            btnGenerate.Enabled = false;
            btnPrint.Enabled = false;
            btnUnDoGenerate.Enabled = false;
        }
        private void txtSalaryProcess_ButtonClick()
        {
            DataSet dsLocal = new DataSet();
            Popup popup = new Popup();
            CLsSalaryProcess objCLsSalaryProcess = new CLsSalaryProcess();
            dsLocal.Tables.Add(objCLsSalaryProcess.GetAllSalaryProcess());
            dsLocal.Tables[0].TableName = "popup";
            popup.Width = 70;
            popup.ShowPopup(null, "SalaryProcessID", ref dsLocal, "popup", "SalaryProcessID,ProcessName,FromDate,ToDate", "", "Salary Process", "ID,Process Name,From Date,To Date", "10,20,10,10");
            if (!string.IsNullOrEmpty(CommonFunction.NullToString(popup.ReturnValue())))
            {
                txtSalaryProcess.Tag = popup.ReturnValue();
                txtSalaryProcess.Text = popup.Field("ProcessName").ToString();
            }
        }
        private void EnableDisableForm()
        {
            ClsSalaryGeneration ObjClsSalaryGeneration = new ClsSalaryGeneration();
            int salaryProcessID = 0;
            int.TryParse(txtSalaryProcess.Tag.ToString(), out salaryProcessID);
            int employeeID = Convert.ToInt32(txtEmployee.Tag);
            if (ObjClsSalaryGeneration.IsSalaryGenerated(salaryProcessID, employeeID))
            {
                btnGenerate.Enabled = false;
                btnUnDoGenerate.Enabled = true;
                btnPrint.Enabled = true;
                chkDeductionApplied.Enabled = false;
            }
            else
            {
                btnGenerate.Enabled = true;
                btnUnDoGenerate.Enabled = false;
                btnPrint.Enabled = false;
                chkDeductionApplied.Enabled = true;
            }
        }
        private void txtEmployee_ButtonClick()
        {
            int salaryProcessID = 0;
            int.TryParse(txtSalaryProcess.Tag.ToString(), out salaryProcessID);
            if (salaryProcessID == 0)
            {
                MessageBox.Show("Select Salary Process Name first", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            DialogResult result = MessageBox.Show("Select Yes to see all employees(Generated) and No to see all employees(Not Generated)", "Info", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            DataSet dsLocal = new DataSet();
            Popup popup = new Popup();
            ClsSalaryGeneration ObjClsSalaryGeneration = new ClsSalaryGeneration();
            if (result == DialogResult.Yes)
            {
                dsLocal.Tables.Add(ObjClsSalaryGeneration.GetAllEmployeeForSalaryALREADYGenerated(salaryProcessID));
            }
            else
            {
                dsLocal.Tables.Add(ObjClsSalaryGeneration.GetAllEmployeeForSalaryNOTGeneration(salaryProcessID));
            }
            dsLocal.Tables[0].TableName = "popup";
            popup.Width = 50;
            popup.ShowPopup(null, "EmployeeID", ref dsLocal, "popup", "EmployeeID,FirstName,LastName", "", "Select Employee", "ID,FirstName,LastName", "5,20,20");
            if (!string.IsNullOrEmpty(CommonFunction.NullToString(popup.ReturnValue())))
            {
                txtEmployee.Tag = popup.ReturnValue();
                txtEmployee.Text = popup.Field("FirstName").ToString() + " " + popup.Field("MiddleName").ToString() + " " + popup.Field("LastName").ToString();
                try
                {
                    GenerateSalary();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                EnableDisableForm();
            }
            else
            {
                txtEmployee.Tag = 0;
                txtEmployee.Text = "";
                ResetForm();
            }
        }

        private void GenerateSalary()
        {
            int salaryProcessID = 0;
            int.TryParse(txtSalaryProcess.Tag.ToString(), out salaryProcessID);
            if (salaryProcessID == 0)
            {
                MessageBox.Show("Select Salary Process Name first", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            ObjClsSalaryGeneration = new ClsSalaryGeneration();
            int employeeID = Convert.ToInt32(txtEmployee.Tag);
            bool chkDeduct = chkDeductionApplied.Checked;
            if (ObjClsSalaryGeneration.IsSalaryGenerated(salaryProcessID, employeeID))
            {
                ObjClsSalaryGeneration.GetAllDetailsForEmployee(employeeID, salaryProcessID, ref chkDeduct, true);
                chkDeductionApplied.Checked = chkDeduct;
                chkDeductionApplied.Enabled = false;
            }
            else
            {
                ObjClsSalaryGeneration.GetAllDetailsForEmployee(employeeID, salaryProcessID, ref chkDeduct, false);
                chkDeductionApplied.Enabled = true;
            }

            lblbalanceLeave.Text = ObjClsSalaryGeneration.employeeInfo.BalanceLeave.ToString();
            lblDepartment.Text = ObjClsSalaryGeneration.employeeInfo.Department;
            lblDesignation.Text = ObjClsSalaryGeneration.employeeInfo.Designation;
            lblEmploymentName.Text = ObjClsSalaryGeneration.employeeInfo.EmploymentName;
            lblEmploymentStatus.Text = ObjClsSalaryGeneration.employeeInfo.EmploymentStatus;
           // lblGender.Text = ObjClsSalaryGeneration.employeeInfo.Gender.ToString();
            lblHourlyRate.Text = ObjClsSalaryGeneration.employeeInfo.HourlyRate.ToString();
            lblJoinigDate.Text = Utility.GetDateToDisplay(ObjClsSalaryGeneration.employeeInfo.JoiningDate);
           // lblMaritalStatus.Text = ObjClsSalaryGeneration.employeeInfo.MaritalStatus.ToString();
            lblName.Text = ObjClsSalaryGeneration.employeeInfo.FullName;
            lblOverTimeRate.Text = ObjClsSalaryGeneration.employeeInfo.OverTimeRate.ToString();
            lblSalaried.Text = ObjClsSalaryGeneration.employeeInfo.Salaried.ToString();
            lblTotalLeave.Text = ObjClsSalaryGeneration.employeeInfo.NoOfLeave.ToString() + " Days";

            lblTotalNoOfWorkingdays.Text = ObjClsSalaryGeneration.TotalNoOfWorkingdays.ToString() + " Days";
            lblTotalDaysWorked.Text = ObjClsSalaryGeneration.TotalDaysWorked.ToString() + " Days";
            lblTotalDaysOff.Text = ObjClsSalaryGeneration.TotalDaysOff.ToString() + " Days";
            lblDaysWithOverTime.Text = ObjClsSalaryGeneration.DaysWithOverTime.ToString() + " Days";
            lblOverTimeWorked.Text = ObjClsSalaryGeneration.OverTimeWorked.ToString() + " Hours";
            lblTotalDays.Text = ObjClsSalaryGeneration.TotalDays.ToString() + " Days";
            lblTotalDaysInThisMonth.Text = ObjClsSalaryGeneration.TotalDaysForThisMonth.ToString() + " Days";
            grdSalaryComponent.DataSource = ObjClsSalaryGeneration.SalaryGenarationInfo;
        }
        private void chkDeductionApplied_CheckedChanged(object sender, EventArgs e)
        {
            if (chkDeductionApplied.Enabled)
            {
                GenerateSalary();
            }
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            int salaryProcessID = 0;
            int.TryParse(txtSalaryProcess.Tag.ToString(), out salaryProcessID);
            if (salaryProcessID == 0)
            {
                MessageBox.Show("Select Salary Process Name first", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            int employeeID = Convert.ToInt32(txtEmployee.Tag);
            try
            {
                ObjClsSalaryGeneration.SaveSalary(employeeID, salaryProcessID, chkDeductionApplied.Checked);
                MessageBox.Show("Salary generated successfully", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                GenerateSalary();
                EnableDisableForm();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            int salaryProcessID = 0;
            int.TryParse(txtSalaryProcess.Tag.ToString(), out salaryProcessID);
            if (salaryProcessID == 0)
            {
                MessageBox.Show("Select Salary Process Name first", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            int employeeID = Convert.ToInt32(txtEmployee.Tag);
            Report frmChangePassword = new Report();
            frmChangePassword.LoadSalaryReport(salaryProcessID, employeeID);
            frmChangePassword.Show(); 


        }

        private void btnUnDoGenerate_Click(object sender, EventArgs e)
        {
            int salaryProcessID = 0;
            int.TryParse(txtSalaryProcess.Tag.ToString(), out salaryProcessID);
            if (salaryProcessID == 0)
            {
                MessageBox.Show("Select Salary Process Name first", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            int employeeID = Convert.ToInt32(txtEmployee.Tag);
            try
            {
                new ClsSalaryGeneration().UndoGenerate(employeeID, salaryProcessID);
                MessageBox.Show("Salary generation successfully cancelled", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtEmployee.Tag = 0;
                txtEmployee.Text = "";
                ResetForm();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void SalaryGeneration_FormClosing(object sender, FormClosingEventArgs e)
        {
            Control[] Cont = this.ParentForm.Controls.Find("groupBox1", false);
            ((GroupBox)Cont[0]).Visible = true;
            Control[] Cont1 = this.ParentForm.Controls.Find("groupBox2", false);
            ((GroupBox)Cont1[0]).Visible = true;

        }


    }
}
